﻿using Microsoft.EntityFrameworkCore.ChangeTracking;

namespace OpenPms.Data;

public class TReservationBase : TBase
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    [Display(Name = "预订单号")]
    public Guid Id { get; set; }
    public Guid RoomTypeId { get; set; }
    [StringLength(32)]
    [Display(Name = "房型")]
    [Obsolete]
    public string RoomTypeName { get; set; } = default!;

    [Display(Name = "房间数量")]
    public int Quantity { get; set; }
    [Display(Name = "预计入住", Description = "预计入住时间")]
    public DateTime DTCheckIn { get; set; }
    [Display(Name = "预计退房", Description = "预计退房时间")]
    public DateTime DTCheckOut { get; set; }
    [Display(Name = "房价码")]
    public Guid RateId { get; set; }
    [StringLength(16)]
    [Display(Name = "房价码")]
    public string RateName { get; set; } = default!;

    [Display(Name = "总费用")]
    public decimal TotalFee { get; set; }
    [Display(Name = "每日房价", Description = "每日单个房间的价格")]
    public List<VPrice> Prices { get; set; } = default!;
    [Display(Name = "已付费")]
    public bool IsPaid { get; set; }
    public bool IsTeam { get; set; }
    [StringLength(32)]
    [Display(Name = "团队名称")]
    public string? TeamName { get; set; }

    /// <summary>
    /// 联系人（预订人或者客人）姓名
    /// </summary>
    [StringLength(32)]
    [Display(Name = "联系人姓名")]
    public string? ContactName { get; set; }
    /// <summary>
    /// 联系人电话
    /// </summary>
    [StringLength(32)]
    [Display(Name = "联系人手机号")]
    public string? ContactMobile { get; set; }

    [Display(Name = "来源代码")]
    public int SouceId { get; set; }
    [Obsolete]
    [Display(Name = "来源名称")]
    [StringLength(16)]
    public string SourceName { get; set; } = "";
    [Display(Name = "Ota订单号", Order = -1)]
    [StringLength(128)]
    public string? OtaOrderId { get; set; }
    [Display(Name = "备注", Order = -1)]
    public string? Memo { get; set; }

}

/// <summary>
/// 预订单
/// 账务绑定预订单
/// </summary>
[EntityTypeConfiguration<TReservationConfig, TReservation>()]
public class TReservation : TReservationBase
{
    [Display(Name = "账务")]
    public List<TAccount> Accounts { get; set; } = default!;
    [Display(Name = "登记单或者入住单")]
    public List<TOrder> Orders { get; set; } = default!;


    public TArchiveReservation Cancel(string? reason)
    {
        var t = typeof(TReservationBase);

        var item = new TArchiveReservation()
        {
            ArchiveType = EArchiveType.取消
        };
        foreach (var p in t.GetProperties())
            p.SetValue(item, p.GetValue(this));
        Memo += "\r\n取消原因：" + reason ?? "无";
        return item;
    }

}

public class TReservationConfig : IEntityTypeConfiguration<TReservation>
{
    public void Configure(EntityTypeBuilder<TReservation> builder)
    {

        builder.Property(ii => ii.Prices).HasConversion(ii => ii.ToJsonString(), ii => ii.To<List<VPrice>>(), new ValueComparer<List<VPrice>>(false));
        builder.HasIndex(ii => new { ii.HotelId, ii.OtaOrderId });
        builder.HasIndex(ii => new { ii.HotelId, ii.ContactName, ii.ContactMobile });
    }
}
